﻿//This should be used in the following way
//_cKeyController = new KeyController();
//_cKeyController.onInitialize(this);
//_cKeyController.createKeyboardCommand("ok",Delegate.create( this, clickField ),"This explanation text",13,false);
//_cKeyController.createKeyboardCommand("upArrow",Delegate.create( this, clickField ),"This explanation text",Key.UP,true);

import mx.utils.Delegate;
import zombie.KeyboardCommand

class zombie.KeyController extends MovieClip{
	private var _aKeyboardCommands:Array;
	private var _aSmoothKeyboardCommands:Array;
	private var _mcParent
	private var _cKeyListener:Object
	
	public function KeyController()
	{
		
	}
	
	public function onInitialize( _mcParent ){
		_aKeyboardCommands = new Array();
		_aSmoothKeyboardCommands = new Array();
		_cKeyListener = new Object();
		_cKeyListener.onKeyDown = Delegate.create( this, pressKey)
		Key.addListener(_cKeyListener);
			
		_mcParent.onEnterFrame = Delegate.create( this, smoothKeyMovement)
	}
	
	function createKeyboardCommand(keyFunctionName,keyFunction,keyFunctionMsg,defaultAsciValue,smooth){
		var cKeyObj = new KeyboardCommand(keyFunctionName,keyFunction,keyFunctionMsg,defaultAsciValue);
		if(!smooth){
			_aKeyboardCommands.push(cKeyObj);
		}
		else {
			_aSmoothKeyboardCommands.push(cKeyObj);
		}
	}

	//////Keyboard action
	function smoothKeyMovement(){
		
		var nLength = _aSmoothKeyboardCommands.length;
		for(var countKeyss = 0;countKeyss<nLength;countKeyss++){
			if (Key.isDown(_aSmoothKeyboardCommands[countKeyss].myAsciKey)) {
				_aSmoothKeyboardCommands[countKeyss].myFunction();
			}
		}
	}

	function pressKey(theKey){
		var theKey = Key.getCode()
		var nLength = _aKeyboardCommands.length
		for(var countKeys = 0;countKeys<nLength;countKeys++){
			if(_aKeyboardCommands[countKeys].myAsciKey == theKey){
				_aKeyboardCommands[countKeys].myFunction();
			}
		}
	}


}